package com.example.teamtribe.hello;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.main.HomeActivity;
import com.example.teamtribe.main.mine.setting.email.EmailActivity;
import com.example.teamtribe.utils.Utility;

public class RegisterActivity extends BaseActivity implements View.OnClickListener, HelloContract.View{

    private EditText etUser;
    private EditText etPsw1;
    private EditText etPsw2;
    private EditText etEmail;
    private EditText etPhone;
    private EditText etCode;
    private Button tvCode;
    private HelloPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etUser = findViewById(R.id.et_user);
        etPsw1 = findViewById(R.id.et_psw1);
        etPsw2 = findViewById(R.id.et_psw2);
        etEmail = findViewById(R.id.et_email);
        etPhone = findViewById(R.id.et_phone);
        etCode = findViewById(R.id.et_code);
        tvCode = findViewById(R.id.tv_send_code);
        presenter = new HelloPresenter(this);

        tvCode.setOnClickListener(this);
        findViewById(R.id.bt_register).setOnClickListener(this);
        findViewById(R.id.bt_login).setOnClickListener(this);
    }

    @Override
    public void updateCode() {
        //没有验证码要更新
    }

    @Override
    public void next() {
        String email = etEmail.getText().toString().trim();
        if (TextUtils.isEmpty(email)) {
            startActivity(new Intent(RegisterActivity.this, HomeActivity.class));
            finish();
        }
        else {
            Intent[] intents = new Intent[2];
            intents[0] = new Intent(RegisterActivity.this, HomeActivity.class);
            intents[1] = new Intent(RegisterActivity.this, EmailActivity.class);
            intents[1].putExtra("email", email);
            startActivities(intents);
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_send_code:
                String phone = etPhone.getText().toString().trim();
                if (phone.length() != 11) {
                    Utility.makeToast("无效手机号码", Toast.LENGTH_SHORT);
                    return;
                }
                new MyCountDownTimer(60 * 1000, 1000).start();
                presenter.sendCode(phone);
                break;

            case R.id.bt_register:
                String username = etUser.getText().toString().trim();
                String psw1 = etPsw1.getText().toString().trim();
                String psw2 = etPsw2.getText().toString().trim();
                String email = etEmail.getText().toString().trim();
                String phoneNum = etPhone.getText().toString().trim();
                String code = etCode.getText().toString().trim();
                presenter.register(username, psw1, psw2, phoneNum, email, code);
                break;

            case R.id.bt_login:
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                finish();
                break;

            default:
                break;
        }
    }

    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            tvCode.setClickable(false);
            tvCode.setTextColor(getResources().getColor(R.color.unable));
            tvCode.setText(l / 1000 + "s后重新发送");
        }

        @Override
        public void onFinish() {
            tvCode.setTextColor(getResources().getColor(R.color.colorAccent));
            tvCode.setText("发送验证码");
            tvCode.setClickable(true);
        }
    }

}